{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JAPoU8Sm5E6e"
   },
   "source": [
    "# Deploying BiqQuery ML Model on Vertex AI Model Registry and Make Predictions\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "132a9ee68ba6"
   },
   "source": [
    "## Learning objectives\n",
    "\n",
    "1. Train a model with BigQuery ML.\n",
    "2. Upload the model to Vertex AI Model Registry.\n",
    "3. Create a Vertex AI Endpoint resource.\n",
    "4. Deploy the Model resource to the Endpoint resource.\n",
    "5. Make prediction requests to the model endpoint.\n",
    "6. Run batch prediction job on the Model resource.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "tvgnzT1CKxrO"
   },
   "source": [
    "## Introduction\n",
    "\n",
    "This notebook demonstrates how to train a model with BigQuery ML and upload it on Vertex AI Model Registry, then make batch predictions.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2de0477b10ce"
   },
   "source": [
    "### Dataset\n",
    "\n",
    "The dataset used for this tutorial is the Penguins dataset from <a href=\"https://cloud.google.com/bigquery/public-data\" target=\"_blank\">BigQuery public datasets</a>. This version of the dataset is used to predict the species of penguins from the available features like culmen-length, flipper-depth etc.\n",
    "\n",
    "Each learning objective will correspond to a __#TODO__ in the notebook, where you will complete the notebook cell's code before running the cell. Refer to the [solution notebook](../solutions/bqml-vertexai-model-registry.ipynb) for reference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "i7EUnXsZhAGF"
   },
   "source": [
    "### Install additional packages\n",
    "\n",
    "Install the following packages required to execute this notebook. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "2b4ef9b72d43"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33m  WARNING: The script tb-gcp-uploader is installed in '/home/jupyter/.local/bin' which is not on PATH.\n",
      "  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# The Vertex AI Workbench Notebook product has specific requirements\n",
    "IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
    "IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
    "    \"/opt/deeplearning/metadata/env_version\"\n",
    ")\n",
    "\n",
    "# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
    "USER_FLAG = \"\"\n",
    "if IS_WORKBENCH_NOTEBOOK:\n",
    "    USER_FLAG = \"--user\"\n",
    "\n",
    "! pip3 install --upgrade google-cloud-aiplatform google-cloud-bigquery pyarrow {USER_FLAG} -q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hhq5zEbGg0XX"
   },
   "source": [
    "### Restart the kernel\n",
    "\n",
    "After you install the additional packages, you need to restart the notebook kernel so it can find the packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "EzrelQZ22IZj"
   },
   "outputs": [],
   "source": [
    "# Automatically restart kernel after installs\n",
    "import os\n",
    "\n",
    "if not os.getenv(\"IS_TESTING\"):\n",
    "    # Automatically restart kernel after installs\n",
    "    import IPython\n",
    "\n",
    "    app = IPython.Application.instance()\n",
    "    app.kernel.do_shutdown(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lWEdiXsJg0XY"
   },
   "source": [
    "## Before you begin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WReHDGG5g0XY"
   },
   "source": [
    "### Set your project ID\n",
    "\n",
    "**If you don't know your project ID**, you can get your project ID using `gcloud`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "o1AuQDpf_hS-"
   },
   "outputs": [],
   "source": [
    "PROJECT_ID = \"[your-project-id]\"  # Replace with your Project ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "RYbBU1jXAETD"
   },
   "outputs": [],
   "source": [
    "if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
    "    # Get your GCP project id from gcloud\n",
    "    shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
    "    PROJECT_ID = shell_output[0]\n",
    "    print(\"Project ID:\", PROJECT_ID)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "set_gcloud_project_id"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Updated property [core/project].\n"
     ]
    }
   ],
   "source": [
    "! gcloud config set project $PROJECT_ID"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "region"
   },
   "source": [
    "#### Region\n",
    "\n",
    "You can also change the `REGION` variable, which is used for operations\n",
    "throughout the rest of this notebook.  Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
    "\n",
    "- Americas: `us-central1`\n",
    "- Europe: `europe-west4`\n",
    "- Asia Pacific: `asia-east1`\n",
    "\n",
    "You might not be able to use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/vertex-ai/docs/general/locations\" target=\"_blank\">Vertex AI regions</a>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "vO3W8YdN2LuA"
   },
   "outputs": [],
   "source": [
    "REGION = \"[your-region]\"  # Replace with your region\n",
    "\n",
    "if REGION == \"[your-region]\":\n",
    "    REGION = \"us-central1\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "29e912d1b106"
   },
   "source": [
    "#### UUID\n",
    "\n",
    "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "c704897922c0"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import string\n",
    "\n",
    "\n",
    "# Generate a uuid of a specifed length(default=8)\n",
    "def generate_uuid(length: int = 8) -> str:\n",
    "    return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
    "\n",
    "\n",
    "UUID = generate_uuid()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XoEqT2Y4DJmf"
   },
   "source": [
    "### Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "pRUOFELefqf1"
   },
   "outputs": [],
   "source": [
    "import google.cloud.aiplatform as aiplatform\n",
    "from google.cloud import bigquery"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "init_aip:mbsdk,all"
   },
   "source": [
    "### Initialize Vertex AI and BigQuery SDKs for Python\n",
    "\n",
    "Initialize the Vertex AI and Big Query SDKs for Python for your project and corresponding bucket."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "BgaYKz2-2LuC"
   },
   "outputs": [],
   "source": [
    "aiplatform.init(project=PROJECT_ID, location=REGION)\n",
    "bqclient = bigquery.Client(project=PROJECT_ID)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lUEtpimzL17Z"
   },
   "source": [
    "## BigQuery ML introduction\n",
    "\n",
    "BigQuery ML (BQML) provides the capability to train ML tabular models, such as classification and regression, in BigQuery using SQL syntax.\n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/bigquery-ml/docs\" target=\"_blank\">BigQuery ML documentation</a>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "30adf1b74bf9"
   },
   "source": [
    "### BigQuery table used for training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "3O7qlOGWNEU4"
   },
   "outputs": [],
   "source": [
    "# Define BigQuery table to be used for training\n",
    "\n",
    "BQ_TABLE = \"bigquery-public-data.ml_datasets.penguins\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "PKQD2e0eMg3M"
   },
   "source": [
    "### Create BigQuery dataset resource\n",
    "First, you create an empty dataset resource in your project."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "luqb-DBiMn-0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DONE\n"
     ]
    }
   ],
   "source": [
    "BQ_DATASET_NAME = \"penguins\" + UUID\n",
    "DATASET_QUERY = f\"\"\"CREATE SCHEMA {BQ_DATASET_NAME}\"\"\"\n",
    "\n",
    "job = bqclient.query(DATASET_QUERY)\n",
    "job.result()\n",
    "print(job.state)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b-_8rZO8NIEb"
   },
   "source": [
    "## Train BigQuery ML model and upload it to Vertex AI Model Registry\n",
    "Next, you create and train a `BigQuery  ML` tabular regression model from the public dataset penguins and store the model in your project `Vertex AI Model Registry` using the `CREATE MODEL` statement. The model configuration is specified in the `OPTIONS` statement as follows:\n",
    "\n",
    "- `model_type`: The type and archictecture of tabular model to train, e.g., LOGISTIC_REG.\n",
    "\n",
    "- `labels`: The column which are the labels.\n",
    "\n",
    "- `model_registry`: To register a BigQuery ML model to Vertex AI Model Registry, you must use `model_registry=\"vertex_ai\"`.\n",
    "\n",
    "Learn more about the <a href=\"https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-create\" target=\"_blank\">CREATE MODEL statement</a>.\n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/bigquery-ml/docs/managing-models-vertex\" target=\"_blank\">Managing BigQuery ML models in the Vertex AI Model Registry</a>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "Q96rlZKRNPjU"
   },
   "outputs": [],
   "source": [
    "# TODO 1\n",
    "# Write the query to create Big Query ML model\n",
    "\n",
    "MODEL_NAME = \"penguins-lr\" + UUID\n",
    "MODEL_QUERY = f\"\"\"\n",
    "# TODO 1: Your code goes here\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eee158e2a375"
   },
   "source": [
    "### Create BigQuery ML Model\n",
    "Create the BigQuery ML model using the query above and the BigQuery client that you created previously:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "LtdieY-BWILs"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Job state: RUNNING\n",
      "Job Error:None\n"
     ]
    }
   ],
   "source": [
    "# Run the model creation query using BigQuery client\n",
    "\n",
    "job = bqclient.query(MODEL_QUERY)\n",
    "print(f\"Job state: {job.state}\\nJob Error:{job.errors}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "e4b007777e68"
   },
   "source": [
    "Check the job status:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "Y4C_3hTEXOE7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DONE\n"
     ]
    }
   ],
   "source": [
    "job.result()\n",
    "print(job.state)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5nHvVttrYfQ8"
   },
   "source": [
    "### Find the model in the Vertex Model Registry\n",
    "\n",
    "You can use the `Vertex AI Model()` method with `model_name` parameter to find the automatically registered model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "E08IwUX5YpAG"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: \"projects/229691399944/locations/us-central1/models/penguins-lrk1wslkxq\"\n",
      "display_name: \"penguins-lrk1wslkxq\"\n",
      "supported_deployment_resources_types: DEDICATED_RESOURCES\n",
      "create_time {\n",
      "  seconds: 1665674928\n",
      "  nanos: 102624000\n",
      "}\n",
      "update_time {\n",
      "  seconds: 1665674928\n",
      "  nanos: 102624000\n",
      "}\n",
      "etag: \"AMEw9yOPM5CNmD7t1f83OMcYiuneVUgvHusxrh3roBE2Y0Sfvu2vwmyuy0aFFb6LzO4=\"\n",
      "version_id: \"1\"\n",
      "version_aliases: \"default\"\n",
      "version_create_time {\n",
      "  seconds: 1665674928\n",
      "  nanos: 102624000\n",
      "}\n",
      "version_update_time {\n",
      "  seconds: 1665675013\n",
      "  nanos: 157346000\n",
      "}\n",
      "model_source_info {\n",
      "  source_type: BQML\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "model = aiplatform.Model(model_name=MODEL_NAME)\n",
    "\n",
    "print(model.gca_resource)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zgvXXlaZaYw3"
   },
   "source": [
    "## Deploy Vertex AI Model resource to a Vertex AI Endpoint resource\n",
    "You must deploy a model to an `endpoint` before that model can be used to serve online predictions; deploying a model associates physical resources with the model so it can serve online predictions with low latency. \n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/vertex-ai/docs/predictions/deploy-model-api#aiplatform_deploy_model_custom_trained_model_sample-python\" target=\"_blank\">Deploy a model using the Vertex AI API</a>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "N27z-_by5gti"
   },
   "source": [
    "### Create a Vertex AI Endpoint resource\n",
    "\n",
    "If you are deploying a model to an existing endpoint, you can skip this cell.\n",
    "\n",
    "- `display_name`: Display name for the endpoint.\n",
    "- `project`: The project ID on which you are creating an endpoint.\n",
    "- `location`: The region where you are using Vertex AI."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "XmmyCtW055Ya"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating Endpoint\n",
      "Create Endpoint backing LRO: projects/229691399944/locations/us-central1/endpoints/2771547756227985408/operations/7341815515234435072\n",
      "Endpoint created. Resource name: projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "To use this Endpoint in another session:\n",
      "endpoint = aiplatform.Endpoint('projects/229691399944/locations/us-central1/endpoints/2771547756227985408')\n",
      "bqml-lr-model-endpointk1wslkxq\n",
      "projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n"
     ]
    }
   ],
   "source": [
    "# TODO 2\n",
    "\n",
    "# Create a Vertex AI endpoint\n",
    "ENDPOINT_DISPLAY_NAME = \"bqml-lr-model-endpoint\" + UUID\n",
    "\n",
    "endpoint = # TODO 2: Your code goes here\n",
    "\n",
    "print(endpoint.display_name)\n",
    "print(endpoint.resource_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "xSCIKdFo56YO"
   },
   "source": [
    "### Deploy the Vertex AI Model resource to Vertex AI Endpoint resource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "f7KfDgALE4aD"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Deploying model to Endpoint : projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "Using default machine_type: n1-standard-2\n",
      "Deploy Endpoint model backing LRO: projects/229691399944/locations/us-central1/endpoints/2771547756227985408/operations/4542828346823671808\n",
      "Endpoint model deployed. Resource name: projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "penguins-lrk1wslkxq\n",
      "projects/229691399944/locations/us-central1/models/penguins-lrk1wslkxq\n"
     ]
    }
   ],
   "source": [
    "# TODO 3\n",
    "DEPLOYED_NAME = \"bqml-lr-penguins\"\n",
    "\n",
    "# Deploy the model using model.deploy method\n",
    "# TODO 3: Your code goes here\n",
    "\n",
    "print(model.display_name)\n",
    "print(model.resource_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "H3K6SVplJ9Mg"
   },
   "source": [
    "## Send prediction request to the Vertex AI Endpoint resource\n",
    "\n",
    "Now that your Vertex AI Model resource is deployed to a Vertex AI `Endpoint` resource, you can do online predictions by sending prediction requests to the `Endpoint` resource.\n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-custom-models\" target=\"_blank\">Get online predictions from custom-trained models</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "2j7ioB3VKEtx"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prediction(predictions=[{'species_probs': [0.004134393613856379, 8.708736467695393e-06, 0.9958568976496759], 'predicted_species': ['Adelie Penguin (Pygoscelis adeliae)'], 'species_values': ['Chinstrap penguin (Pygoscelis antarctica)', 'Gentoo penguin (Pygoscelis papua)', 'Adelie Penguin (Pygoscelis adeliae)']}], deployed_model_id='6705354069805891584', model_version_id='1', model_resource_name='projects/229691399944/locations/us-central1/models/penguins-lrk1wslkxq', explanations=None)\n"
     ]
    }
   ],
   "source": [
    "# TODO 4\n",
    "instance = {\n",
    "    \"island\": \"Dream\",\n",
    "    \"culmen_length_mm\": 36.6,\n",
    "    \"culmen_depth_mm\": 18.4,\n",
    "    \"flipper_length_mm\": 184.0,\n",
    "    \"body_mass_g\": 3475.0,\n",
    "    \"sex\": \"FEMALE\",\n",
    "}\n",
    "\n",
    "prediction = # TODO 4: Your code goes here\n",
    "print(prediction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "C39qOaBHZI1G"
   },
   "source": [
    "## Batch Prediction on the BigQuery ML model\n",
    "Here you request batch predictions directly from the BigQuery ML model; you don't need to deploy the model to an endpoint. For data types that support both batch and online predictions, use batch predictions when you don't require an immediate response and want to process accumulated data by using a single request.\n",
    "\n",
    "Learn more abount <a href=\"https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-predict\" target=\"_blank\">The ML.PREDICT function</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "id": "_QxZb19_o6jx"
   },
   "outputs": [],
   "source": [
    "# TODO 5\n",
    "# Run batch prediction job on the Model\n",
    "sql_ml_predict = f\"\"\"SELECT * FROM ML.PREDICT(MODEL `{PROJECT_ID}.{BQ_DATASET_NAME}.{MODEL_NAME}`, \n",
    "(SELECT\n",
    "      *\n",
    "    FROM\n",
    "      `{BQ_TABLE}` LIMIT 10))\"\"\"\n",
    "\n",
    "job = bqclient.query(sql_ml_predict)\n",
    "prediction_result = # TODO 5: Your code goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "id": "TVpsLI5nrVii"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>predicted_species</th>\n",
       "      <th>predicted_species_probs</th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>culmen_length_mm</th>\n",
       "      <th>culmen_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>[{'label': 'Chinstrap penguin (Pygoscelis anta...</td>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>Dream</td>\n",
       "      <td>46.9</td>\n",
       "      <td>16.6</td>\n",
       "      <td>192.0</td>\n",
       "      <td>2700.0</td>\n",
       "      <td>FEMALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Adelie Penguin (Pygoscelis adeliae)</td>\n",
       "      <td>[{'label': 'Adelie Penguin (Pygoscelis adeliae...</td>\n",
       "      <td>Adelie Penguin (Pygoscelis adeliae)</td>\n",
       "      <td>Dream</td>\n",
       "      <td>36.6</td>\n",
       "      <td>18.4</td>\n",
       "      <td>184.0</td>\n",
       "      <td>3475.0</td>\n",
       "      <td>FEMALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>[{'label': 'Chinstrap penguin (Pygoscelis anta...</td>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>Dream</td>\n",
       "      <td>46.5</td>\n",
       "      <td>17.9</td>\n",
       "      <td>192.0</td>\n",
       "      <td>3500.0</td>\n",
       "      <td>FEMALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Adelie Penguin (Pygoscelis adeliae)</td>\n",
       "      <td>[{'label': 'Adelie Penguin (Pygoscelis adeliae...</td>\n",
       "      <td>Adelie Penguin (Pygoscelis adeliae)</td>\n",
       "      <td>Dream</td>\n",
       "      <td>40.9</td>\n",
       "      <td>18.9</td>\n",
       "      <td>184.0</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>MALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>[{'label': 'Chinstrap penguin (Pygoscelis anta...</td>\n",
       "      <td>Chinstrap penguin (Pygoscelis antarctica)</td>\n",
       "      <td>Dream</td>\n",
       "      <td>49.5</td>\n",
       "      <td>19.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>3800.0</td>\n",
       "      <td>MALE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           predicted_species  \\\n",
       "0  Chinstrap penguin (Pygoscelis antarctica)   \n",
       "1        Adelie Penguin (Pygoscelis adeliae)   \n",
       "2  Chinstrap penguin (Pygoscelis antarctica)   \n",
       "3        Adelie Penguin (Pygoscelis adeliae)   \n",
       "4  Chinstrap penguin (Pygoscelis antarctica)   \n",
       "\n",
       "                             predicted_species_probs  \\\n",
       "0  [{'label': 'Chinstrap penguin (Pygoscelis anta...   \n",
       "1  [{'label': 'Adelie Penguin (Pygoscelis adeliae...   \n",
       "2  [{'label': 'Chinstrap penguin (Pygoscelis anta...   \n",
       "3  [{'label': 'Adelie Penguin (Pygoscelis adeliae...   \n",
       "4  [{'label': 'Chinstrap penguin (Pygoscelis anta...   \n",
       "\n",
       "                                     species island  culmen_length_mm  \\\n",
       "0  Chinstrap penguin (Pygoscelis antarctica)  Dream              46.9   \n",
       "1        Adelie Penguin (Pygoscelis adeliae)  Dream              36.6   \n",
       "2  Chinstrap penguin (Pygoscelis antarctica)  Dream              46.5   \n",
       "3        Adelie Penguin (Pygoscelis adeliae)  Dream              40.9   \n",
       "4  Chinstrap penguin (Pygoscelis antarctica)  Dream              49.5   \n",
       "\n",
       "   culmen_depth_mm  flipper_length_mm  body_mass_g     sex  \n",
       "0             16.6              192.0       2700.0  FEMALE  \n",
       "1             18.4              184.0       3475.0  FEMALE  \n",
       "2             17.9              192.0       3500.0  FEMALE  \n",
       "3             18.9              184.0       3900.0    MALE  \n",
       "4             19.0              200.0       3800.0    MALE  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(prediction_result.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TpV-iwP9qw9c"
   },
   "source": [
    "## Cleaning up\n",
    "\n",
    "To clean up all Google Cloud resources used in this project, you can <a href=\"https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects\" target=\"_blank\">delete the Google Cloud\n",
    "project</a> you used for the tutorial.\n",
    "\n",
    "Learn more about <a href=\"https://cloud.google.com/bigquery-ml/docs/managing-models-vertex\" target=\"_blank\">Deleting BigQuery ML models from Vertex AI Model Registry</a>\n",
    "\n",
    "Otherwise, you can delete the individual resources you created in this tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "id": "sx_vKniMq9ZX"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Undeploying Endpoint model: projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "Undeploy Endpoint model backing LRO: projects/229691399944/locations/us-central1/endpoints/2771547756227985408/operations/3468719835695808512\n",
      "Endpoint model undeployed. Resource name: projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "Deleting Endpoint : projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n",
      "Delete Endpoint  backing LRO: projects/229691399944/locations/us-central1/operations/4151578129195859968\n",
      "Endpoint deleted. . Resource name: projects/229691399944/locations/us-central1/endpoints/2771547756227985408\n"
     ]
    }
   ],
   "source": [
    "# Delete the endpoint using the Vertex endpoint object\n",
    "endpoint.undeploy_all()\n",
    "endpoint.delete()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "id": "3c5c8dc2f597"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<google.cloud.bigquery.table._EmptyRowIterator at 0x7f88499c3210>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Delete BigQuery ML model\n",
    "\n",
    "delete_query = f\"\"\"DROP MODEL `{PROJECT_ID}.{BQ_DATASET_NAME}.{MODEL_NAME}`\"\"\"\n",
    "job = bqclient.query(delete_query)\n",
    "job.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "id": "85dfc88f5472"
   },
   "outputs": [],
   "source": [
    "# Delete the created BigQuery dataset\n",
    "! bq rm -r -f $PROJECT_ID:$BQ_DATASET_NAME"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "bqml-vertexai-model-registry.ipynb",
   "toc_visible": true
  },
  "environment": {
   "kernel": "python3",
   "name": "tf2-gpu.2-6.m97",
   "type": "gcloud",
   "uri": "gcr.io/deeplearning-platform-release/tf2-gpu.2-6:m97"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
